import { Component, OnDestroy, ViewChild } from '@angular/core';
import { Util } from '../../../util/util';
import { Subscription } from 'rxjs/Subscription';
import { ManhuaDetailProvider } from './manhua.detail.provider';
import { ManhuaFavorComponent } from '../../../component/manhua-favor/manhua-favor';
import { Router } from '@angular/router';


@Component({
    selector: 'manhua-detail',
    templateUrl: './manhua.detail.html',
    styleUrls: ['./manhua.detail.css'],
    providers: [ManhuaDetailProvider]
})

export class ManhuaDetail {

    @ViewChild('ManhuaFavor') ManhuaFavor: ManhuaFavorComponent;

    manhua: any;
    isFavor: boolean = false;

    constructor(private util: Util, private service: ManhuaDetailProvider, private router: Router) {
        this.manhua = this.util.sessionStore().get('manhuaDetail');

        this.getHttp(this.manhua);
    }

    // favor组件emit isFavor
    FavorEmitOn(data) {
        console.log(data);

        this.isFavor = data;
    }

    // 获取漫画详情
    ajaxSub: Subscription;
    getHttp(param) {
        let json = {
            "_id": param['_id']
        };

        this.ajaxSub = this.service.getManhuaDetail(json).subscribe((res: any) => {
            if (res.success && res.data.length > 0) {

                let _manhua = res.data[0];

                if(_manhua.domain === "www.1kkk.com"||_manhua.domain === "http://www.1kkk.com"){
                    _manhua.chapters = _manhua.chapters.reverse();
                }

                //   设置防盗图片的中转
                if (_manhua.domain === "manhua.dmzj.com"||_manhua.domain === "http://manhua.dmzj.com") {
                    
                    _manhua.img = "/api/manhua/img?url=" + _manhua.img;
                }

                this.manhua = _manhua;

                console.log('45');
                console.log(this.ManhuaFavor);

            }
        });
    }


    // 记录用户阅读历史章节
    ajaxHistorySub: Subscription;
    goToRead(_chapter) {
        console.log(this.isFavor);
        console.log(_chapter);
        let json = {
            "title": this.manhua.title,
            "img": this.manhua.img,
            "_id": this.manhua['_id'],
            "readChapter": _chapter['chapter'],
            "readChapterHref": _chapter['href'],
            "chapterLength": this.manhua['chapters'].length || 0
        };


        console.log(this.ManhuaFavor.isFavor);

        // manhuaFavor
        if (this.ManhuaFavor.isFavor) {
            this.ManhuaFavor.updateFavor(_chapter);
        }


        this.ajaxHistorySub = this.service.addManhuaHistory(json).subscribe((res: any) => {

            let imageData = Object.assign({ "cur_chapter": _chapter }, this.manhua);

            this.util.sessionStore().set('manhuaImageData', imageData);

            this.router.navigate(['/manhua', this.manhua.title, _chapter['chapter']]);

        }, (err: any) => { });



    }

    ngOnDestroy() {
        if (this.ajaxHistorySub) {
            this.ajaxHistorySub.unsubscribe();
        }

        if (this.ajaxSub) {
            this.ajaxSub.unsubscribe();
        }
    }
}